package cz.czu.pef.DataModelling;

import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Index;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cz.czu.pef.DataModelling.utils.Lazy;

@Entity
@Table(name="TRAININGS")
@ManagedBean
@ViewScoped
@Component
public class Training extends IdPersistenceObject
{
	private static final long serialVersionUID = -1120811474121576871L;

	@Column(nullable=false)
	@Index(name="IDX_TRAININGS_LAST_PASS")
	private Date lastPass;

	@Column(nullable=true)
	private Date evidencePassExpiration;

	@ManyToOne(fetch = FetchType.EAGER, targetEntity=TrainingType.class, optional=false, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
	private TrainingType trainingType;

	@SuppressWarnings("unused")
	private Training()
	{
	}

	public Training(TrainingType trainingType, Date evidencePassExpiration, Date lastPass)
	{
		this.setTrainingType(trainingType);
		this.setEvidencePassExpiration(evidencePassExpiration);
		this.setLastPass(lastPass);
	}

	public TrainingType getTrainingType()
	{
		return trainingType;
	}

	
	public void setTrainingType(TrainingType trainingType)
	{
		this.trainingType = trainingType;
	}

	
	public Date getEvidencePassExpiration()
	{
		return evidencePassExpiration;
	}

	
	public void setEvidencePassExpiration(Date evidencePassExpiration)
	{
		this.evidencePassExpiration = evidencePassExpiration;
	}

	public Date getLastPass()
	{
		return lastPass;
	}

	
	public void setLastPass(Date lastPass)
	{
		this.lastPass = lastPass;
	}

	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("Training [lastPass=");
		builder.append(lastPass);
		builder.append(", passExpiration=");
		builder.append(evidencePassExpiration);
		builder.append(", trainingType=");
		builder.append(trainingType);
		builder.append(", toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}

	/**
	 * Can be deleted (no relation).
	 */
	@Override
	public boolean canBeRemoved()
	{
		return true;
	}
}
